package cn.gok.oa.email.service.impl;

import cn.gok.oa.email.dto.EmailDto;
import cn.gok.oa.email.entity.Emails;
import cn.gok.oa.email.entity.MailRecipients;
import cn.gok.oa.email.mapper.EmailsMapper;
import cn.gok.oa.email.mapper.MailRecipientsMapper;
import cn.gok.oa.email.service.IEmailsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gok
 * @since 2022-08-09
 */
@Service
public class EmailsServiceImpl extends ServiceImpl<EmailsMapper, Emails> implements IEmailsService {
    @Autowired
    MailRecipientsMapper mailRecipientsMapper;
    @Autowired
    EmailsMapper emailsMapper;
    @Override
    @Transactional
    public Boolean sendEmail(EmailDto emailDto) {
        Emails emails = new Emails();
        emails.setUid(emailDto.getUid());
        emails.setTitle(emailDto.getTitle());
        emails.setTextbody(emailDto.getTextbody());
        if(save(emails))
        {
            MailRecipients mailRecipients = new MailRecipients();
            mailRecipients.setEid(emails.getId());
            mailRecipients.setUid(emailDto.getSid());
            if(mailRecipientsMapper.insert(mailRecipients)>0)
                return true;
            else
                return false;
        }
        return false;
    }

    @Override
    public List<Emails> getSend(Integer id) {
        Map<String, Object> map = new HashMap<>();
        map.put("uid", id);
        return emailsMapper.selectByMap(map);
    }

    @Override
    public List<Emails> selectReceive(Integer id) {
        return emailsMapper.selectReceive(id);
    }
}
